const $api = require('./api/index')

App({
    onLaunch: function () {
        //获取定位消息
        wx.getSetting({
            success(res) {
                if (res.authSetting["scope.userLocation"] == false) {
                    wx.setStorageSync('userLocation', res.authSetting["scope.userLocation"])
                } else {
                    wx.setStorageSync('userLocation', true)
                }
            }
        })

        this.login()
    },
    login: function () {
        var that = this;
        wx.login({
            success(res) {
                if (res.code) {
                    var datas = {}
                    datas.code = res.code
                    datas.appType = that.globalData.apptype
                    $api.login(datas).then((res) => {
                        that.sucs(res)
                    }).catch((res) => {
                        console.log(res);
                    });
                } else {
                    console.log('登录失败！' + res.data.errMsg)
                }
            }
        })
    },
	
    sucs: function (e) {
        if (e.code == 200) {
            var that = this
            var userInfoarr = e.data

            userInfoarr['telnum'] = e.data.phone;
            wx.setStorageSync('userInfo', userInfoarr)
            wx.setStorageSync('openId', userInfoarr.openId) //缓存openId
            var datas = {}
            $api.userInfo(datas).then((e) => {
                var listdata = e.data
                listdata.telnum = listdata.phone
                wx.setStorageSync('userInfo', listdata)
                wx.setStorageSync('openId', listdata.openId) //缓存openId
            }).catch((res) => {
                console.error(res);
            })
        }
    },
    globalData: {
        userInfo: wx.getStorageSync('userInfo'),
        serverTime: '', //服务器时间
        global: 1,
        apptype: 19,
        openId: wx.getStorageSync('openId'),
    }
})
